#include <Windows.h>
#include <GL/GL.h>
#include "IL/il.h"
#include "IL/ilu.h"
#include "Image.h"

namespace Caelum {

Image::Image(std::wstring FilePath)
{
	uint ilimg;
	ilGenImages(1, &ilimg);
	ilBindImage(ilimg);
	if (!ilLoadImage(FilePath.c_str()))
		throw new ImageNotFoundException(L"The image file \"" + FilePath + L"\" was not found.");

	glGenTextures(1, &mGLImg);
	glBindTexture(GL_TEXTURE_2D, mGLImg);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT),
		0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
	ilDeleteImages(1, &ilimg);
}

Image::~Image()
{
	glDeleteTextures(1, &mGLImg);
}

void Image::GetSize(uint &Width, uint &Height)
{
	Bind();
	Width = mWidth;
	Height = mHeight;
}

void Image::Bind()
{
	glBindTexture(GL_TEXTURE_2D, mGLImg);
}

}